package com.leji.song.mainPage;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import com.dgl.sdk.view.LoadImage;
import com.leji.song.R;
import com.leji.song.mainPage.model.DS_ShopDetail;

import java.util.List;

/**
 * Created by mac on 2014/6/14.
 */
public class ShopAdapter extends BaseAdapter {
    protected ListItemView listItemView;
    protected LayoutInflater listContainer;
    protected Context context;
    protected List<DS_ShopDetail> shopList;

    public ShopAdapter(){};

    public ShopAdapter(Context context, List<DS_ShopDetail> shopList){
        this.context = context;
        this.shopList = shopList;

        listContainer = LayoutInflater.from(context);

    }


    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return shopList.size();
    }

    @Override
    public Object getItem(int position) {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return 0;
    }

    protected  class ListItemView{
        public ImageView shopImg;
        public TextView shopName;
        public TextView license;
        public TextView sellTime;
        public TextView faraway;


    }
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        // 自定义视图
        listItemView = new ListItemView();
        // 获取list_item布局文件的视图
        if (convertView == null) {
            convertView = listContainer.inflate(R.layout.food_sell_adapter, null);
            // 获取控件对象
            listItemView.shopImg = (ImageView) convertView.findViewById(R.id.shopImg);
            listItemView.shopName = (TextView) convertView.findViewById(R.id.shopName);
            listItemView.license = (TextView) convertView
                    .findViewById(R.id.license);
            listItemView.sellTime = (TextView) convertView
                    .findViewById(R.id.sellTime);
            listItemView.faraway = (TextView) convertView
                    .findViewById(R.id.faraway);

            // 设置控件集到convertView
            convertView.setTag(listItemView);
        } else {
            listItemView = (ListItemView) convertView.getTag();
        }


        LoadImage.display(listItemView.shopImg, shopList.get(position).getImgUrl());
        listItemView.shopName.setText(shopList.get(position).getName());
        if(shopList.get(position).getLicense().equals("1")){
            listItemView.license.setText("卫生许可");
        }else{
            listItemView.license.setText("卫生未许可");
        }

        listItemView.sellTime.setText("营业时间：" +
                shopList.get(position).getStarttime() + "-"
                + shopList.get(position).getEndtime());

        listItemView.faraway.setText(shopList.get(position).getDis());

        convertView.setOnClickListener(new MyClickListener(position));

        return convertView;
    }

    class MyClickListener implements View.OnClickListener {
        private int position;

        public MyClickListener(int position){
            this.position = position;
        }
        @Override
        public void onClick(View v) {
            Intent intent = new Intent(context, ShopMenuActivity.class);
            intent.putExtra("shopID", shopList.get(position).getId());
            intent.putExtra("shopName", shopList.get(position).getName());
            context.startActivity(intent);
        }

    }
}
